package com.pricetag.helpers;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

public class GPSLocation {
	private Context cont;

	public GPSLocation(Context contextt) {
		this.cont = contextt;
	}

	public Location getCurrentLocation() {

		LocationManager locationManager;
		// String service=Context.LOCATION_SERVICE;
		locationManager = (LocationManager) cont.getSystemService(cont.LOCATION_SERVICE);
		// String gpsProvider=LocationManager.GPS_PROVIDER;
		String gpsProvider = LocationManager.NETWORK_PROVIDER;
		// check
		Criteria decideCriteria = new Criteria();
		decideCriteria.setAccuracy(Criteria.ACCURACY_FINE);
		decideCriteria.setAltitudeRequired(false);
		decideCriteria.setPowerRequirement(Criteria.POWER_LOW);
		decideCriteria.setBearingRequired(false);
		decideCriteria.setCostAllowed(true);
		decideCriteria.setSpeedRequired(false);

		String locationProviderSelected = locationManager.getBestProvider(decideCriteria, true);
		Log.d("NeX", "Provider being used " + gpsProvider);
		// check
		locationManager.requestLocationUpdates(gpsProvider, 0, 0, locationListener);
		Location location = locationManager.getLastKnownLocation(gpsProvider);
		return location;
		// String output=newLocation(location);

		// return output;
	}

	private final LocationListener locationListener = new LocationListener() {
		public void onLocationChanged(Location location) {
			newLocation(location);
		}

		public void onProviderDisabled(String provider) {
		}

		public void onProviderEnabled(String provider) {
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	};

	private String newLocation(Location loc) {
		// String outPut = "Location Not Found";
		String output = null;
		if (loc != null) {
			double latitude = loc.getLatitude();
			double longitude = loc.getLongitude();
			// outPut = "Lat:" + latitude + "\nLong:" + longitude;
			output = latitude + "-" + longitude;
		}
		return output;

		// textView.setText("Your Current Position is:\n" +
		// outPut);
	}

}
